package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class useSQL {
    //增加数据
    //0失败，1添加成功，2添加失败
    public static int Add(String table, String text1, String text2, String text3, String text4) {
        String SQL_USER_INSET = "insert into `talk_with_friends`." + table + " VALUES (" + text1 + ",'" + text2 + "','" + text3 + "','" + text4 + "');";
        int flag = 0;
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        try {
            //连接数据库，获得连接对象
            conn = BaseDao.getConnection();
            //创建预编译环境
            preparedStatement = conn.prepareStatement(SQL_USER_INSET);
            int line = preparedStatement.executeUpdate();
            if (line > 0) {
                flag = 1;
            } else {
                flag = 2;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    //删除数据
    //返回修改行数，未查找到返回-1
    public static int Delete(String tbname, String linename, String id) {
        if (!Select(tbname, linename, id)) {
            return -1;
        }
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        try {
            //连接数据库，获得连接对象
            conn = BaseDao.getConnection();
            //创建预编译环境
            preparedStatement = conn.prepareStatement("delete from " + tbname + " where " + linename + " = '" + id + "' ;");
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //修改数据
    //返回修改行数，未查找到返回-1，SQL语句未发生修改则返回-2
    public static int Updeat(String tbname, String linename, String id, String line1, String line2, String line3, String text1, String text2, String text3) {
        //判断是否存在
        if (!Select(tbname, linename, id)) {
            return -1;
        }
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        String SQL = null;
        if (line2 != null) {
            if (line3 != null) {
                SQL = "update " + tbname + " set " + line1 + " = '" + text1 + "'," + line2 + " = '" + text2 + "'," + line3 + " = '" + text3 + "' where " + linename + " = '" + id + "' ;";
            } else {
                SQL = "update " + tbname + " set " + line1 + " = '" + text1 + "'," + line2 + " = '" + text2 + "' where " + linename + " = '" + id + "' ;";
            }
        } else {
            SQL = "update " + tbname + " set " + line1 + " = '" + text1 + "' where " + linename + " = '" + id + "' ;";
        }
        if (SQL == null) {
            return -2;
        }
        try {
            //连接数据库，获得连接对象
            conn = BaseDao.getConnection();
            //创建预编译环境
            preparedStatement = conn.prepareStatement(SQL);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //查询数据是否存在
    public static boolean Select(String tbname, String linename, String text) {
        Connection conn = null;
        boolean flag = false;
        try {
            //连接数据库，获得连接对象
            conn = BaseDao.getConnection();
            Statement statement = conn.createStatement();
            //执行sql的查询语句，得到结果集
            ResultSet reusult = statement.executeQuery("select * from " + tbname + " where " + linename + " = '" + text + "' ; ");
            if (reusult.next()) {
                flag = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
}
